package com.system.cipher;

import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class SecurityUtils {
	public static final String CHARSET = "UTF-8";
	private static final Log logger = LogFactory.getLog(SecurityUtils.class);

	/**
	 * 生成加密串
	 * 
	 * @param source
	 * @param key
	 * @return
	 * @throws UnsupportedEncodingException
	 * @throws GeneralSecurityException
	 */
	public static String encryptAuthenticator(String source, String key)
			throws UnsupportedEncodingException, GeneralSecurityException {
		byte[] input = source.getBytes(CHARSET);
		byte[] output = Base64.encode(TripleDES.encrypt(input, key));
		return new String(output, CHARSET);
	}

	/**
	 * 生成加密串
	 * 
	 * @param source
	 * @param key
	 * @return
	 * @throws UnsupportedEncodingException
	 * @throws GeneralSecurityException
	 */
	public static String encryptAuthenticatorNew(String source, String key)
			throws UnsupportedEncodingException, GeneralSecurityException {
		byte[] input = source.getBytes(CHARSET);
		byte[] output = Base64.encode(TripleDES
				.encrypt(SHA1.encode(input), key));
		return new String(output, CHARSET);
	}

	/**
	 * 生成加密串
	 * 
	 * @param source
	 * @param key
	 * @return
	 * @throws UnsupportedEncodingException
	 * @throws GeneralSecurityException
	 */
	public static String encryptAuthenticator(byte[] source, String key)
			throws UnsupportedEncodingException, GeneralSecurityException {
		byte[] input = source;
		byte[] output = Base64.encode(TripleDES.encrypt(input, key));
		return new String(output, CHARSET);
	}

	public static String decryptAuthenticator(String source, String key)
			throws UnsupportedEncodingException, GeneralSecurityException {
		byte[] input = source.getBytes(CHARSET);
		byte[] output = TripleDES.decrypt(Base64.decode(input), key);
		return new String(output, CHARSET);
	}

	public static String generateDigest(String source)
			throws UnsupportedEncodingException, GeneralSecurityException {
		byte[] input = source.getBytes(CHARSET);
		byte[] output = Base64.encode(SHA1.encode(input));
		return new String(output, CHARSET);
	}

	public static void checkDigest(String source, String digest)
			throws Exception, UnsupportedEncodingException,
			GeneralSecurityException {
		String _digest = generateDigest(source);
		if (!_digest.equals(digest)) {
			throw new Exception( "加密串摘要验证失败，加密信息不可信");
		}
	}

	public static void checkAuthentication(String authenticatorInput,
			String source, String key) throws Exception {
		String authenticator = null;
		try {
			authenticator = encryptAuthenticator(source, key);
			logger.info("authenticator:" + authenticator);
		} catch (Exception e) {
			throw new Exception("解密失败");
		}
		if (!authenticator.equals(authenticatorInput)) {
			throw new Exception("加密串校验失败");
		}
	}

	public static void checkAuthenticationNew(String authenticatorInput,
			String source, String key) throws Exception {
		String authenticator = null;
		try {
			authenticator = encryptAuthenticatorNew(source, key);
			logger.info("authenticator:" + authenticator);
		} catch (Exception e) {
			throw new Exception( "解密失败");
		}
		if (!authenticator.equals(authenticatorInput)) {
			throw new Exception("加密串校验失败");
		}
	}

	public static void main(String[] args) {
		String key="363A5689E956DD22E3859E70100272153509624ED927260E";
		String aa = "2009-03-26 18:55:3831000061345";
		String bb="";
		try {
			
			String cc = encryptAuthenticator(aa,key);
			bb=encryptAuthenticator(cc, key);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (GeneralSecurityException e) {
			e.printStackTrace();
		}
		System.out.println(aa);
		System.out.println(bb);
	}
}
